<template>
  <div>
    <!-- 按钮 -->
    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="rangeChange"></el-button>
    <!-- 表格
      data 展示数据 —— 数组类型
    -->
    <el-button @click="handleExport2" style="margin-right: 50px;">导出报表</el-button>
    <!-- <el-date-picker value-format="yyyy-MM" v-model="value" type="month" placeholder="选择月">
    </el-date-picker> -->
    <el-date-picker v-model="valueData" value-format="yyyy-MM-dd" type="daterange" align="right" unlink-panels
      range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
    </el-date-picker>
    <el-button style="margin-left: 10px;" icon="el-icon-search" @click="rangeChange"></el-button>

    <!--   <el-table id="table" style="width: 100%;" border :data="list">
      <el-table-column prop="id" label="订单id" align="center">
      </el-table-column>
      <el-table-column prop="operateUserNickname" label="用户昵称" align="center">
      </el-table-column>
      <el-table-column label="充值类型" align="center"
        :filters="[{ text: '充值金币', value: '1' }, { text: '余额提现', value: '2' }, { text: '充值会员', value: '3' }]"
        :filter-method="filterTag" filter-placement="bottom-end">
        <template slot-scope="{ row, $index }">
          <p v-if="row.operateType == 1">充值金币</p>
          <p v-else-if="row.operateType == 2">余额提现</p>
          <p v-else>充值会员</p>
        </template>
      </el-table-column>
      <el-table-column prop="operateMoney" label="金额" align="center">
      </el-table-column>
      <el-table-column prop="datOptdt" label="时间" align="center">
      </el-table-column>
    </el-table> -->
    <el-table :span-method="objectSpanMethod" id="table" style="width: 80%" :data="getValues" :show-header="false"
      border>
      <el-table-column align="center" v-for="(item, index) in getHeaders" :key="index" :prop="item">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  // import moment from 'moment'
  // moment(row.extensionStartTime * 1000).format("YYYY-MM-DD");
  import {
    exportToExcel
  } from '@/utils/Export2Excel'
  export default {
    name: 'summaryTable',
    data() {
      return {
        headers: [{
            prop: 'date',
            label: '查询月份',
          }, {
            prop: 'type',
            label: '项目',
          },
          // 用户
          {
            prop: 'totalUserNum',
            label: '用户',
          }, {
            prop: 'totalUserMoney',
            label: '用户',
          },
          {
            prop: 'monthUserMoney',
            label: '用户',
          },
          // 充值
          {
            prop: 'rechargeVip',
            label: '充值',
          }, {
            prop: 'rechargeGold',
            label: '充值',
          },
          {
            prop: 'rechargeAdv',
            label: '充值',
          },

          // 收入一
          {
            prop: 'messageCostMoney',
            label: '收入一',
          }, {
            prop: 'audioCostMoney',
            label: '收入一',
          },
          {
            prop: 'videoCostMoney',
            label: '收入一',
          },
          {
            prop: 'giftCostGold',
            label: '收入一',
          },
          {
            prop: 'hotCostGold',
            label: '收入一',
          },
          {
            prop: 'rateGold',
            label: '收入一',
          },
          {
            prop: 'totalCostGold',
            label: '收入一',
          },
          // 收入二
          {
            prop: 'totalVipMoney',
            label: '收入二',
          },
          // 收入三
          {
            prop: 'totalAdvMoney',
            label: '收入三',
          },
          // 佣金
          {
            prop: 'messageMoney',
            label: '佣金',
          },
          {
            prop: 'audioMoney',
            label: '佣金',
          },
          {
            prop: 'videoMoney',
            label: '佣金',
          },
          {
            prop: 'giftMoney',
            label: '佣金',
          },

          // 推广费
          {
            prop: 'promotionGold',
            label: '推广费',
          },
          {
            prop: 'promotionVip',
            label: '推广费',
          },
          {
            prop: 'promotionAdv',
            label: '推广费',
          },
          {
            prop: 'promotionInvite',
            label: '推广费',
          },
          // 手续费
          {
            prop: 'commissionWX',
            label: '手续费',
          },
          {
            prop: 'commissionZFB',
            label: '手续费',
          },
          {
            prop: 'commissionIOS',
            label: '手续费',
          },
          // 税金
          {
            prop: 'taxes',
            label: '税金',
          },
          // 累计未核销
          {
            prop: 'unverifiedGold',
            label: '累计未核销',
          },
          {
            prop: 'unverifiedVip',
            label: '累计未核销',
          },
          {
            prop: 'unverifiedAdv',
            label: '累计未核销',
          },
          //平台汇总
          {
            prop: 'platformMoney',
            label: '平台汇总',
          },
          {
            prop: 'platformMoneyWX',
            label: '平台汇总',
          },
          {
            prop: 'platformMoneyZFB',
            label: '平台汇总',
          },
          {
            prop: 'platformBalance',
            label: '平台汇总',
          },
          {
            prop: 'platformIncome',
            label: '平台汇总',
          },
          {
            prop: 'platformPay',
            label: '平台汇总',
          },
          {
            prop: 'platformGrossProfit',
            label: '平台汇总',
          }
        ],
        tableData: [{
            date: '',
            type: '类目',
            totalUserNum: '用户总数量',
            totalUserMoney: '用户总核销金额',
            monthUserMoney: '月人均核销金额',

            rechargeVip: '充值金额（会员）',
            rechargeGold: '充值金额（金币）',
            rechargeAdv: '充值金额（广告）',

            messageCostMoney: '核销聊天金额',
            audioCostMoney: '核销语音聊天金额',
            videoCostMoney: '核销视频聊天金额',
            giftCostGold: '核销礼物打赏金额',
            hotCostGold: '核销作品加热金额',
            rateGold: '金币摊销率',
            totalCostGold: '核销金币金额',
            totalVipMoney: '核销会员金额',
            totalAdvMoney: '核销广告金额',

            messageMoney: '聊天抽成',
            audioMoney: '语音聊天抽成',
            videoMoney: '视频聊天抽成',
            giftMoney: '打赏抽成',


            promotionGold: '充值金币推广费',
            promotionVip: '充值会员推广费',
            promotionAdv: '购买广告推广费',
            promotionInvite: '邀请新人推广费',


            commissionWX: '微信手续费',
            commissionZFB: '支付宝手续费',
            commissionIOS: '苹果手续费',

            taxes: '提现代扣税金',

            unverifiedGold: '未核销金额（金币）',
            unverifiedVip: '未核销金额（会员）',
            unverifiedAdv: '未核销金额（广告）',


            platformMoney: '本月总提现金额',
            platformMoneyWX: '本月总提现金额（微信）',
            platformMoneyZFB: '本月总提现金额（支付宝）',
            platformBalance: '本月未提现金额',
            platformIncome: '平台总收入',
            platformPay: '平台总支出',
            platformGrossProfit: '平台毛利',
          },
          {
            type: '数据',
            totalUserNum: 0,
            totalUserMoney: 0,
            monthUserMoney: 0,
            rechargeVip: 0,
            rechargeGold: 0,
            rechargeAdv: 0,
            messageCostMoney: 0,
            audioCostMoney: 0,
            videoCostMoney: 0,
            giftCostGold: 0,
            hotCostGold: 0,
            rateGold: 0,
            totalCostGold: 0,

            totalVipMoney: 0,
            totalAdvMoney: 0,

            messageMoney: 0,
            audioMoney: 0,
            videoMoney: 0,
            giftMoney: 0,

            promotionGold: 0,
            promotionVip: 0,
            promotionAdv: 0,
            promotionInvite: 0,

            unverifiedGold: 0,
            unverifiedVip: 0,
            unverifiedAdv: 0,

            commissionWX: 0,
            commissionZFB: 0,
            commissionIOS: 0,

            taxes: 0,

            platformMoney: 0,
            platformMoneyWX: 0,
            platformMoneyZFB: 0,
            platformBalance: 0,
            platformIncome: 0,
            platformPay: 0,
            platformGrossProfit: 0,
          }
        ],
        valueData: [],
        value: '',
        //页码
        pageNumber: 1,
        //每页数量
        pageSize: 10000,
        //总条数
        total: 0,
        //列表数据
        list: [],

        spanArr: [],
        userNumList: [],
      }
    },
    computed: {
      getHeaders() {
        return this.tableData.reduce((pre, cur, index) => pre.concat(`value${index}`), ['title'])
      },
      getValues() {
        return this.headers.map(item => {
          return this.tableData.reduce((pre, cur, index) => Object.assign(pre, {
            ['value' + index]: cur[item.prop]
          }), {
            'title': item.label,
          });
        });
      }
    },
    mounted() {
      this.getSpanArr()
      // 获取当前时间
      const today = new Date();
      // 获取当前时间(today)的年份
      const year = today.getFullYear();
      // 获取月份
      const month = String(today.getMonth() + 1).padStart(2, '0');
      // // 获取当前日
      // const day = String(today.getDate()).padStart(2, '0');
      // // 得到年月日
      // const thisDayDate = `${year}-${month}-${day}`; //打印当前日期
      console.log(year, month)
      let ym = year + '-' + month
      this.valueData[0] = ym + '-' + '01'
      this.valueData[1] = ym + '-' + this.getLastDayOfMonth(year, month)
      this.tableData[0].date = ym
      // console.log(this.valueData)
      // this.goldCostTable()
    },
    methods: {
      rangeChange() {
        this.value = this.valueData[0].split('-')[0] + '-' + this.valueData[0].split('-')[1]
        console.log('时间', this.value)
        if (!this.value) {
          return;
        }
        // this.valueData[0] = this.value + '-' + '01'
        // this.valueData[1] = this.value + '-' + this.getLastDayOfMonth(this.value.split('-')[0], this.value.split('-')[
        //   1])
        console.log('时间范围', this.valueData, this.tableData)
        this.tableData[0].date = this.value
        if (this.checkMonth(this.valueData[0], this.valueData[1]) == 1) {
          this.valueData[1] = this.addDate(this.valueData[1])
          this.getGoldTable()
          this.getGoldTableVipAdv()
        } else {
          this.$message({
            message: '日期选择只能同一个月内',
            type: 'error',
            duration: 5 * 1000
          })
        }
        // this.goldCostTable()

      },
      addDate(strDate) {
        let dateFormat = new Date(strDate);
        dateFormat = dateFormat.setDate(dateFormat.getDate() + 1);
        dateFormat = new Date(dateFormat);
        let y = dateFormat.getFullYear()
        let m = (dateFormat.getMonth() + 1).toString().padStart(2, '0')
        let d = dateFormat.getDate().toString().padStart(2, '0')
        return `${y}-${m}-${d}`
      },
      checkMonth(dt1, dt2) {
        dt1 = new Date(dt1.replace(/-/g, "/"));
        dt2 = new Date(dt2.replace(/-/g, "/"));
        if (dt1.getFullYear() == dt2.getFullYear() && dt1.getMonth() == dt2.getMonth()) {
          console.log('同')
          return 1
        } else {
          console.log('不同')
          return 0
        }
      },

      async getGoldTableVipAdv() {
        let res = await this.$API.running.getGoldTable({
          month: this.value
        })
        console.log('金币参考22', res)
        if (res.code == 200) {
          // for (var i = 0; i < res.data.length; i++) {
          //   if (res.data[i].type == 2) {
          //     var dataListAdv = res.data[i]
          //   }
          // }
          var wxRate = 0
          var alipayRate = 0
          var iosRate = 0
          for (var i = 0; i < res.data.length; i++) {
            wxRate += res.data[i].wxRate
            alipayRate += res.data[i].alipayRate
            iosRate += res.data[i].iosRate
            // console.log(res.data[i].alipayRate)
            // console.log(res.data[i].iosRate)
            // console.log(res.data[i].wxRate)
          }
          console.log('手续费', wxRate, alipayRate, iosRate)


          this.tableData[1].commissionWX = wxRate.toFixed(2)
          this.tableData[1].commissionZFB = alipayRate.toFixed(2)
          this.tableData[1].commissionIOS = iosRate.toFixed(2)

          // for (var i = 0; i < res.data.length; i++) {
          //   if (res.data[i].type == 3) {
          //     var dataListVip = res.data[i]
          //   }
          // }
          var dataListAdv = res.data.filter(function(item) {
            return item.type == 2
          })
          var dataListVip = res.data.filter(function(item) {
            return item.type == 3
          })

          console.log(dataListAdv, dataListVip)
          if (dataListAdv.length > 0) {
            this.tableData[1].unverifiedAdv = dataListAdv[0].totalUnuseMoney
            // console.log('@@@@@@@@@',dataListAdv[0].totalUnuseMoney,this.tableData[1].unverifiedAdv)
          } else {
            this.tableData[1].unverifiedAdv = 0
          }
          if (dataListVip.length > 0) {
            this.tableData[1].unverifiedVip = dataListVip[0].totalUnuseMoney
          } else {
            this.tableData[1].unverifiedVip = 0
          }


        }
      },

      async getGoldTable() {
        var mo = Number(this.value.split('-')[1] - 1)
        var ye = this.value.split('-')[0]
        if (mo == 0) {
          mo = 12
          ye = ye - 1
        }
        let res = await this.$API.running.getGoldTable({
          month: ye + '-' + mo
        })
        console.log('金币参考', res)
        // if (res.code == 200) {
        //   // this.usableRate = res.data.usableRate
        //   this.getPageAccountDetailList2(res.data)
        // }

        if (res.code == 200) {
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].type == 1) {
              var dataListGold = res.data[i]
            }
          }
          // console.log(dataListGold)
          this.getPageAccountDetailList2(dataListGold)
        }
      },
      async getPageAccountDetailList2(lastMonth) {
        let res = await this.$API.running.goldTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-金币明细表2', res)
        res.data.unverifiedGold = lastMonth.totalUnuseGold + res.data.unverifiedGold + res.data.useGold

        if (this.valueData[0] == '2023-12-01' && this.valueData[1] == '2024-01-01') {
          res.data.useGold = res.data.useGold + 10000
        }
        res.data.totalMoy = res.data.rechargeMoney + lastMonth.totalUnuseMoney
        console.log('摊销率', res.data.totalMoy * 100 / res.data.unverifiedGold)
        // res.data.rate = this.toPercent((res.data.totalMoy * 100 / res.data.unverifiedGold).toFixed(4))
        this.usableRate = (res.data.totalMoy * 100 / res.data.unverifiedGold).toFixed(4)
        res.data.useGoldMoy = ((res.data.useGold / 100) * (res.data.totalMoy * 100 / res.data.unverifiedGold))
          .toFixed(2)
        this.tableData[1].unverifiedGold = (res.data.totalMoy - res.data.useGoldMoy).toFixed(2)
        // this.tableData[1].unverifiedGold = res.data.rechargeMoney
        this.goldCostTable()
      },
      toPercent(point) {
        var str = Number(point * 100).toFixed(2);
        str += "%";
        return str;
      },
      // 获取某年某月的最后一天
      getLastDayOfMonth(year, month) {
        var date = new Date(year, month - 1, '01');
        //设置日期
        date.setDate(1);
        //设置月份
        date.setMonth(date.getMonth() + 1);
        //获取本月的最后一天
        var cdate = new Date(date.getTime() - 1000 * 60 * 60 * 24);
        //返回结果
        return cdate.getDate();
      },

      async goldCostTable() {
        let res = await this.$API.running.goldCostTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-金币核销统计表', res)
        // let arr = []
        // for (var i = 0; i < res.data.length; i++) {
        //   arr.push(res.data[i].fromUserNickName)
        // }
        // console.log(arr)
        // const map = new Map();
        // const newArr = res.data.filter(v => !map.has(v.fromUserNickName) && map.set(v.fromUserNickName, v));
        // console.log(newArr)
        let event = res.data
        for (var j in event) {
          this.userNumList.push(
            Object.assign(event[j], {
              userName: event[j].fromUserNickName,
            })
          );
        }
        // console.log(this.userNumList)

        //聊天
        let sum = 0;
        res.data.map(function(item) {
          sum += item.dayMessageCostMoney;
        });
        // console.log(sum.toFixed(2))

        //语音
        let sum1 = 0;
        res.data.map(function(item) {
          sum1 += item.dayAudioCostMoney;
        });
        // console.log(sum1.toFixed(2))

        //视频
        let sum2 = 0;
        res.data.map(function(item) {
          sum2 += item.dayVideoCostMoney;
        });
        // console.log(sum2.toFixed(2))

        //打赏
        let sum3 = 0;
        res.data.map(function(item) {
          sum3 += item.dayGiftCostMoney;
        });
        // console.log(sum3.toFixed(2))




        this.tableData[1].messageCostMoney = sum.toFixed(2)
        this.tableData[1].audioCostMoney = sum1.toFixed(2)
        this.tableData[1].videoCostMoney = sum2.toFixed(2)
        this.tableData[1].giftCostGold = sum3.toFixed(2)




        this.memberRechargeTable()

        this.userRewardTable()
      },

      async userRewardTable() {
        let res = await this.$API.running.userRewardTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-用户收益明细表', res)

        //佣金 - 聊天抽成
        let sum4 = 0;
        res.data.map(function(item) {
          sum4 += item.messageMoney;
        });
        // console.log(sum4.toFixed(2))

        //佣金 - 语音聊天抽成
        let sum5 = 0;
        res.data.map(function(item) {
          sum5 += item.audioMoney;
        });

        //佣金 - 视频聊天抽成
        let sum6 = 0;
        res.data.map(function(item) {
          sum6 += item.videoMoney;
        });

        //佣金 - 礼物打赏抽成
        let sum7 = 0;
        res.data.map(function(item) {
          sum7 += item.giftMoney;
        });

        //提现代扣税金
        let sum8 = 0;
        res.data.map(function(item) {
          sum8 += item.withdrawMoney;
        });

        this.tableData[1].messageMoney = sum4.toFixed(2)
        this.tableData[1].audioMoney = sum5.toFixed(2)
        this.tableData[1].videoMoney = sum6.toFixed(2)
        this.tableData[1].giftMoney = sum7.toFixed(2)
        this.tableData[1].taxes = (sum8 * 0.03).toFixed(2)
        this.getMembersList()
      },

      async getMembersList() {
        let res = await this.$API.running.getAllUserDistributionByTimeDesc({
          pageNumber: 1,
          pageSize: 100000,
          orderBY: '',
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        var arr = res.data.rows.filter(function(item) {
          return item.toUserNickName != ''
        })
        var newArr = [...new Set(arr.map(i => i.nickName))]; // 去重的时候需要注意和普通数组不同
        console.log(newArr);
        var list = [];
        newArr.forEach(i => {
          list.push(arr.filter(t => t.nickName === i));
        })
        var mlist = [];
        list.forEach((i, index) => {
          mlist.push({
            userNickName: newArr[index],
            num: i.length,
            moy: this.getMoy(i.length)
          })
        })
        console.log('财务统计表-用户邀请', mlist);
        //提现代扣税金
        let sum = 0;
        mlist.map(function(item) {
          sum += item.moy;
        });
        this.tableData[1].promotionInvite = sum.toFixed(2)
      },
      getMoy(n) {
        if (n >= 50 && n <= 99) {
          return 50
        } else if (n >= 100 && n <= 149) {
          return 80
        } else if (n >= 150 && n <= 299) {
          return 180
        } else if (n >= 300 && n <= 499) {
          return 280
        } else if (n >= 500) {
          return 500
        } else {
          return 0
        }
      },

      async memberRechargeTable(arr) {
        let res = await this.$API.running.memberRechargeTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-会员充值表', res, arr)

        //核销会员金额
        let sum = 0;
        res.data.map(function(item) {
          sum += item.monthUseMoney;
        });
        console.log(sum.toFixed(2))
        this.tableData[1].totalVipMoney = sum.toFixed(2)

        // //充值会员金额
        // let sum1 = 0;
        // res.data.map(function(item) {
        //   sum1 += item.rechargeMoney;
        // });
        // console.log(sum1.toFixed(2))
        // this.tableData[1].rechargeVip = sum1.toFixed(2)

        // //充值会员本月核销额
        // let sum2 = 0;
        // res.data.map(function(item) {
        //   sum2 += item.monthUseMoney;
        // });
        // console.log(sum2.toFixed(2))
        // this.tableData[1].unverifiedVip = (Number(this.tableData[1].rechargeVip) - Number(this.tableData[1]
        //   .totalVipMoney)).toFixed(2)
        // this.tableData[1].unverifiedVip = 0

        let event = res.data
        for (var j in event) {
          this.userNumList.push(
            Object.assign(event[j], {
              userName: event[j].memberNickName,
            })
          );
        }
        console.log(this.userNumList)
        this.advertisementRecordTable()
      },

      async advertisementRecordTable() {
        let res = await this.$API.running.advertisementRecordTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-广告核销明细表', res)

        //核销广告金额
        let sum = 0;
        res.data.map(function(item) {
          sum += item.monthUseMoney;
        });
        console.log(sum.toFixed(2))
        this.tableData[1].totalAdvMoney = sum.toFixed(2)

        // //充值广告金额
        // let sum1 = 0;
        // res.data.map(function(item) {
        //   sum1 += item.rechargeMoney;
        // });
        // console.log(sum1.toFixed(2))
        // this.tableData[1].rechargeAdv = sum1.toFixed(2)


        // //剩余可用广告金额
        // let sum2 = 0;
        // res.data.map(function(item) {
        //   sum2 += item.advertisementBalance;
        // });
        // console.log(sum2.toFixed(2))

        // this.tableData[1].unverifiedAdv = sum2.toFixed(2)
        // this.tableData[1].unverifiedAdv = 0

        let event = res.data
        for (var j in event) {
          this.userNumList.push(
            Object.assign(event[j], {
              userName: event[j].userNickName,
            })
          );
        }
        console.log(this.userNumList)

        this.dynamicExtensionTable()

        // const map = new Map();
        // const newArr = this.userNumList.filter(v => !map.has(v.userName) && map.set(v.userName, v));
        // console.log('人数', newArr)
        // this.tableData[1].totalUserNum = newArr.length
        // this.tableData[1].totalUserMoney = (Number(this.tableData[1].totalVipMoney) + Number(this.tableData[1]
        //   .totalAdvMoney) + Number(this.tableData[1]
        //   .totalCostGold)).toFixed(2)
        // this.tableData[1].monthUserMoney = ((Number(this.tableData[1].totalVipMoney) + Number(
        //   this.tableData[1]
        //   .totalAdvMoney) + Number(this.tableData[1]
        //   .totalCostGold)) / this.tableData[1].totalUserNum).toFixed(2)

      },

      async dynamicExtensionTable() {
        let res = await this.$API.running.dynamicExtensionTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-作品加热表', res)
        //加热
        let sum = 0;
        res.data.map(function(item) {
          sum += item.costMoney;
        });
        console.log(sum.toFixed(2))
        this.tableData[1].hotCostGold = sum.toFixed(2)

        let event = res.data
        for (var j in event) {
          this.userNumList.push(
            Object.assign(event[j], {
              userName: event[j].userNickName,
            })
          );
        }
        console.log(this.userNumList)
        const map = new Map();
        const newArr = this.userNumList.filter(v => !map.has(v.userName) && map.set(v.userName, v));
        console.log('人数', newArr)
        this.tableData[1].totalUserNum = newArr.length

        // this.getGoldTable()
        this.goldTable()
      },
      // async getGoldTable() {
      //   let res = await this.$API.running.getGoldTable({
      //     month: this.value
      //   })
      //   console.log('金币参考', res)
      //   this.usableRate = res.data.usableRate
      //   this.goldTable()
      // },

      async goldTable() {
        let res = await this.$API.running.goldTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-金币明细表', res)
        // this.$nextTick(() => {
        this.tableData[1].totalCostGold = ((Number(this.tableData[1].messageCostMoney) + Number(this.tableData[
                1]
              .audioCostMoney) +
            Number(this.tableData[1].videoCostMoney) + Number(this.tableData[1].giftCostGold) + Number(this
              .tableData[
                1].hotCostGold)) *
          (this.usableRate)).toFixed(2)
        // console.log('totalCostGold', this.tableData[1].totalCostGold, (1 - this.tableData[1].rateGold))
        // })
        // this.tableData[1].rechargeGold = res.data.rechargeMoney

        // this.tableData[1].unverifiedGold = res.data.unverifiedGold / 100



        this.tableData[1].totalUserMoney = (Number(this.tableData[1].totalVipMoney) + Number(this.tableData[1]
          .totalAdvMoney) + Number(this.tableData[1]
          .totalCostGold)).toFixed(2)
        this.tableData[1].monthUserMoney = ((Number(this.tableData[1].totalVipMoney) + Number(
          this.tableData[1]
          .totalAdvMoney) + Number(this.tableData[1]
          .totalCostGold)) / this.tableData[1].totalUserNum).toFixed(2)


        this.tableData[1].platformIncome = this.tableData[1].totalUserMoney
        // this.tableData[1].rateGold = this.toPercent(res.data.promotionRate)

        this.tableData[1].rateGold = this.toPercent(this.usableRate)

        this.userDistributionTable()
      },


      async userDistributionTable() {
        let res = await this.$API.running.userDistributionTable({
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('财务统计表-分销明细表', res)

        let a = 0
        let b = 0
        let c = 0
        for (var i = 0; i < res.data.length; i++) {
          // console.log(res.data[i])
          if (res.data[i].type == '充值金币') {
            a = a + res.data[i].distributionMoney
          }
          if (res.data[i].type == '充值会员') {
            b = b + res.data[i].distributionMoney
          }
          if (res.data[i].type == '购买广告') {
            c = c + res.data[i].distributionMoney
          }
        }
        // console.log(a, b, c)
        this.tableData[1].promotionGold = a.toFixed(2)
        this.tableData[1].promotionVip = b.toFixed(2)
        this.tableData[1].promotionAdv = c.toFixed(2)

        this.getPageAccountDetailList()

      },

      getSpanArr() {
        var data = this.headers
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            this.spanArr.push(1);
            this.pos = 0
          } else {
            // 判断当前元素与上一个元素是否相同
            if (data[i].label === data[i - 1].label) {
              this.spanArr[this.pos] += 1;
              this.spanArr.push(0);
            } else {
              this.spanArr.push(1);
              this.pos = i;
            }
          }
        }
      },
      objectSpanMethod({
        row,
        column,
        rowIndex,
        columnIndex
      }) {
        // if (columnIndex === 0) {
        //   if (rowIndex % 2 === 0) {
        //     return {
        //       rowspan: 2,
        //       colspan: 1
        //     };
        //   } else {
        //     return {
        //       rowspan: 0,
        //       colspan: 0
        //     };
        //   }
        // }
        if (columnIndex === 0) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col
          }
        }
      },
      // rangeChange() {
      //   console.log('时间范围', this.valueData)
      //   console.log(this.tableData)
      //   this.tableData[0].sDate = this.valueData[0]
      //   this.tableData[0].eDate = this.valueData[1]
      //   console.log(this.tableData)
      // },
      handleExport2() {
        let auditResult = prompt('报表名字：');
        if (auditResult != null && auditResult != "") {
          exportToExcel('#table', auditResult);
        }
      },
      async getPageAccountDetailList() {
        let res = await this.$API.running.getPageAccountDetailList({
          pageNumber: this.pageNumber,
          pageSize: this.pageSize,
          endTime: this.valueData[1],
          startTime: this.valueData[0]
        })
        console.log('旧表提现', res)


        // 总金币
        let listGold = res.data.rows.filter(function(item) {
          return item.operateType == 1
        })
        let cGold = 0
        for (var i = 0; i < listGold.length; i++) {
          cGold = cGold + listGold[i].operateMoney
        }

        // 总广告
        let listAdv = res.data.rows.filter(function(item) {
          return item.operateType == 2
        })

        let cAdv = 0
        for (var i = 0; i < listAdv.length; i++) {
          cAdv = cAdv + listAdv[i].operateMoney
        }

        // 总会员
        let listVip = res.data.rows.filter(function(item) {
          return item.operateType == 3
        })

        let cVip = 0
        for (var i = 0; i < listVip.length; i++) {
          cVip = cVip + listVip[i].operateMoney
        }


        // 总提现微信
        let list1 = res.data.rows.filter(function(item) {
          return item.operateType == 5
        })
        // console.log('提现', list1)
        let c = 0
        for (var i = 0; i < list1.length; i++) {
          c = c + list1[i].operateMoney
        }

        // 总提现支付宝
        let list2 = res.data.rows.filter(function(item) {
          return item.operateType == 6
        })
        // console.log('提现', list1)
        let c2 = 0
        for (var i = 0; i < list2.length; i++) {
          c2 = c2 + list2[i].operateMoney
        }

        this.tableData[1].rechargeGold = cGold.toFixed(2)
        this.tableData[1].rechargeAdv = cAdv.toFixed(2)
        this.tableData[1].rechargeVip = cVip.toFixed(2)


        this.tableData[1].platformMoneyWX = c.toFixed(2)
        this.tableData[1].platformMoneyZFB = c2.toFixed(2)
        this.tableData[1].platformMoney = Number(this.tableData[1].platformMoneyWX) + Number(this.tableData[1]
          .platformMoneyZFB)

        this.tableData[1].platformBalance = ((Number(this.tableData[1].rechargeAdv) + Number(this.tableData[1]
          .rechargeGold) + Number(this.tableData[1]
          .rechargeVip)).toFixed(2)) - this.tableData[1].platformMoney

        // this.total = res.data.sumLine;
        // this.list = res.data.rows;
        // this.list.push(res.data.rows[0]);
        this.tableData[1].platformPay = (Number(this.tableData[1].messageMoney) +
          Number(this.tableData[1].audioMoney) + Number(this.tableData[1]
            .videoMoney) + Number(this.tableData[1].giftMoney) + Number(this.tableData[1]
            .promotionAdv) + Number(this.tableData[1]
            .promotionVip) + Number(this.tableData[1]
            .promotionGold) + Number(this.tableData[1]
            .promotionInvite) + Number(this.tableData[1]
            .commissionWX) + Number(this.tableData[1]
            .commissionZFB) + Number(this.tableData[1]
            .commissionIOS) + Number(this.tableData[1]
            .taxes)).toFixed(2)

        this.tableData[1].platformGrossProfit = (this.tableData[1].platformIncome - (Number(this.tableData[1]
            .messageMoney) +
          Number(this.tableData[1].audioMoney) + Number(this.tableData[1]
            .videoMoney) + Number(this.tableData[1].giftMoney) + Number(this.tableData[1]
            .promotionAdv) + Number(this.tableData[1]
            .promotionVip) + Number(this.tableData[1]
            .promotionGold) + Number(this.tableData[1]
            .taxes))).toFixed(2)


      },
      handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.pageSize = val
      },
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.pageNumber = val
      },
      filterTag(value, row) {
        console.log(value, row)
        return row.operateType == value;
      },
    }
  }
</script>

<style>

</style>